package chapter14.interfaces;

import java.lang.reflect.Field;

/**
 * @author : DuJiabao
 * @Date : 2021/3/4 10:05
 * @Project : thinking_in_java
 * @File : null.java
 * @Desc :
 */
public class ModifyingPrivateFields {
  public static void main(String[] args) throws Exception {
    WithPrivateFinalField pf = new WithPrivateFinalField();
    System.out.println(pf);

    Field i = pf.getClass().getDeclaredField("i");  // 获取指定属性
    i.setAccessible(true);
    System.out.println("i.getInt(pf) " + i.getInt(pf));
    i.setInt(pf, 10);
    System.out.println(pf);

    Field s = pf.getClass().getDeclaredField("s");
    s.setAccessible(true);
    System.out.println("s.get(pf) "+s.get(pf));
    s.set(pf, "No, you're not");   // final域没办法修改！！！
    System.out.println(pf);

    Field s2 = pf.getClass().getDeclaredField("s2");
    s2.setAccessible(true);
    System.out.println("s2.get(pf) "+s2.get(pf));
    s2.set(pf, "No, you're not");
    System.out.println(pf);
  }
}
